package com.focusai.efairy.ui.adapter.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * adapter基类
 */
public abstract class EBaseAdapter<ViewHolder, Data> extends BaseAdapter {
    private Context context;
    private List<Data> datas;
    private ViewHolder t;
    private LayoutInflater inflater;

    public EBaseAdapter(Context context, List<Data> datas) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public Data get(int i) {
        return datas.get(i);
    }

    public List<Data> getDatas() {
        return datas;
    }

    public void setDatas(List<Data> datas) {
        this.datas = datas;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        Data data = null;
        if (i < datas.size()) {
            data = datas.get(i);
        }
        if (view == null) {
            view = createView(inflater, viewGroup, i);
            viewHolder = createViewHolder(view, data, i);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        setViewHolder(view, viewHolder, data, i);
        return view;
    }

    public Context getContext() {
        return context;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    public abstract ViewHolder createViewHolder(View view, Data data, int i);

    public abstract View createView(LayoutInflater inflater, ViewGroup viewGroup, int i);

    public abstract void setViewHolder(View view, ViewHolder viewHolder, Data data, int i);
}
